var config=require('../../config.js');
var common=require('../utils/common.js');
var promise = require('../utils/es6-promise.auto.min.js');
function get_material(curDate,page){

  //获取上一天的
  wx.request({
    url: config.materials + '/search/findByTagNameAndTypeAndShowTimeBefore',
    data:{
      tagName:config.tagName,
      type:'VIDEO',
      showTime: curDate,
      page:0,
      size:1,
      sort: "showTime,desc"
    }, header: {
      'content-type': 'application/json'
    },success:function(res){
      var materials = res.data._embedded.materials;
      if (materials.length > 0) {
        page.setData({
          pre_id: materials[0].id
        });
      } else {
        page.setData({
          pre_id: 0
        });
      }
    }
  });
  //获取下一天的
  wx.request({
    url: config.materials + '/search/findByTagNameAndTypeAndShowTimeAfter',
    data: {
      tagName: config.tagName,
      type: 'VIDEO',
      showTime: curDate,
      page: 0,
      size: 1
    }, header: {
      'content-type': 'application/json'
    },success: function (res) {
      var materials = res.data._embedded.materials;
      if(materials.length>0){
        page.setData({
        next_id: materials[0].id
        });
      }else{
        page.setData({
          next_id:0
        });
      }
    }
  })
}

/**
 * 收藏或是移除收藏
 * id 
 * action 为add或remove,cb  回掉函数
 */
function add_rem_favorite2(id, name, action,page,value) {
  wx.request({
    url: config.materials + '/' + id + '/favor',
    data: {
      username: name,
      action: action
    },
    method: 'get',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      if ("success" == res.data) {
        page.setData({
          favid:value
        });
      }
    }
  });
}
//页面结束的时候操作
function add_rem_favorite(id, name, action) {
  wx.request({
    url: config.materials + '/' + id + '/favor',
    data: {
      username: name,
      action: action
    },
    method: 'get',
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      if ("success" == res.data) {
        
      }
    }
  });
}
//一个系列下的所有视频
function load_series_materialsList(materialId,page){
  wx.request({
    url: config.materials + '/tv/findListByMaterialsId',
    data: {
      materialId: materialId
    },
    header: {
      'content-type': 'application/json'
    },
    success: function (res) {
      var data = res.data;
      page.setData({
        load_series_materialsList: data
      });
      loadPre_Next(materialId,page);
    }
  });    
}

function loadPre_Next(id, page){
  var load_series_materialsList=page.data.load_series_materialsList;
  if (load_series_materialsList.length>1){
    for (var i = 0; i < load_series_materialsList.length;i++){
      if (id == load_series_materialsList[i].material_id){
        if (i == 0) {
          page.setData({
            pre_id: 0,
            next_id: load_series_materialsList[i+1].material_id
          });
        } else if (i == load_series_materialsList.length - 1) {//表示是末尾一条数据
          page.setData({
            pre_id: load_series_materialsList[i-1].material_id,
            next_id: 0
          });
        } else {
          page.setData({
            pre_id: load_series_materialsList[i-1].material_id,
            next_id: load_series_materialsList[i+1].material_id,
          });
        }
      }
    }

  }else{
    page.setData({
      pre_id:0,
      next_id:0
    });
  }

}

module.exports = {
  get_material: get_material,
  add_rem_favorite: add_rem_favorite,
  load_series_materialsList: load_series_materialsList,
  loadPre_Next: loadPre_Next
}